package com.tenton.servlet;

import com.tenton.pojo.ShoppingCart;
import com.tenton.service.CartService;
import com.tenton.service.Impl.CartServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Date: 2021/1/20
 * @Author: Tenton
 * @Description: 删除购物车中对应商品
 */
@WebServlet(name = "DeleteCartBookServlet",urlPatterns = "/deleteCartBookServlet")
public class DeleteCartBookServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置编码格式，防止乱码
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        CartService cartService = new CartServiceImpl();
        //获取购物车Id
        int id = Integer.parseInt(request.getParameter("cartId"));
        //根据购物车Id删除图书
        int result = cartService.deleteCart(id);
        //判断是否删除成功
        if (result == 1){
            response.sendRedirect("/listCartServlet");
        }else {
            response.getWriter().write("删除失败，2秒后自动跳转到主界面");
            response.setHeader("Refresh","3;URL=/listCartServlet");
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
